<?php
namespace Controllers;

require_once('\Views\PageView.php');
require_once('\Views\LoginView.php');
require_once('\Views\ViewAlbumsView.php');
require_once('\Controllers\LoginController.php');
require_once('\Models\LoginModel.php');
require_once('\Models\Database.php');
require_once('\Models\AlbumCatalog.php');
require_once('\Models\UserCatalog.php');
require_once('\Models\ImageCatalog.php');

class GalleryController
{	
	public function createControl()
	{
		$db = new \Models\Database();
		$pv = new \Views\PageView();
		$lm = new \Models\LoginModel();
		$vav= new \Views\ViewAlbumsView();
		$ac	= new \Models\AlbumCatalog();
		$uc	= new \Models\UserCatalog();
		$ic	= new \Models\ImageCatalog();
		$lc = new \Controllers\LoginController();
		
		$html = $lc->createControl($db, new \Views\LoginView());
		
		// Databasuppkopplingsfel om false
		if(!$html)
		{
			$html .= $pv->setStatusText(\Common\Strings::DB_CONNECTION_ERROR, false);
			return $pv->generateHTMLPage(\Common\Strings::TITLE_CREATE_ALBUM, $html);
		}
		
		if($lm->IsLoggedIn())
		{
			$html .= $pv->getMenu();
			
			// Hämtar användare
			$user = $uc->getUser($db, $lm->getUserId());
			
			// Hämtar id och album
			$albumId = $pv->getNumericValue($pv->albumId, false);
			$album = $ac->getAlbum($db, $albumId);
			
			// Avbryt rendering om inget album finns kopplat till id:t
			if($album == null)
			{
				$html .= $pv->setStatusText(\Common\Strings::DB_ALBUM_MISSING, false);
				return $pv->generateHTMLPage(\Common\Strings::TITLE_VIEW_ALBUM, $html);
			}
			
			$html .= $pv->encapsulateString("h1", $album->Title());
			$html .= $vav->getAlbumInfo($album);
						
			// Hämtar albumbilder
			$images = $ic->getImages($db, $albumId);
			$html .= $vav->getAlbumImages($images);
		}
		
		$db->Close();
		
		return $pv->generateGalleryPage(\Common\Strings::TITLE_VIEW_ALBUM, $html);
	}
}
?>